package h0908;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 对称二叉树
 * @date 2024/9/8 14:29
 */
public class BinaryTreeDuichen {
    public static void main(String[] args) {

    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);
        while (!queue.isEmpty()) {
            TreeNode a = queue.poll();
            TreeNode b = queue.poll();
            if (a == null && b == null) continue;
            if (a == null || b == null) return false;
            if (a.val != b.val) {
                return false;
            }
            queue.offer(a.left);
            queue.offer(b.right);
            queue.offer(a.right);
            queue.offer(b.left);
        }
        return true;
    }

    public boolean isSymmetric2(TreeNode root) {
        if (root == null) return true;

        return isMirror(root.left,root.right);
    }

    private boolean isMirror(TreeNode t1, TreeNode t2) {
        if (t1==null&&t2==null) {
            return true;
        }
        if (t1==null||t2==null){
            return false;
        }
        return (t1.val==t2.val)&&isMirror(t1.left,t2.right)&&isMirror(t1.right,t2.left);
    }
}
